<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="材料描述" prop="describes">
        <el-input
          v-model="queryParams.describes"
          placeholder="请输入材料描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="党支部" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入党支部"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material:mark:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="markList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="材料编号" align="center" prop="materialId" sortable/>
      <el-table-column label="材料描述" align="center" prop="describes" />
      <el-table-column label="党支部" align="center" prop="deptName" sortable/>
      <el-table-column label="上传人" align="center" prop="uploadBy" sortable/>
      <el-table-column label="上传时间" align="center" prop="uploadTime" sortable/>
      <el-table-column label="评分" align="center" prop="score" sortable/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.score"
            :colors="colors"
            @change="handleMark(scope.row)">
          </el-rate>
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDownload(scope.row)"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMark, getMark, delMark, addMark, updateMark } from "@/api/material/mark";

export default {
  name: "Mark",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资料评分表格数据
      markList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        score: null
      },
      form:{},
      // 表单校验
      rules: {
      },
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资料评分列表 */
    getList() {
      this.loading = true;
      listMark(this.queryParams).then(response => {
        this.markList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        materialId: null,
        score: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateMark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMark(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material/mark/export', {
        ...this.queryParams
      }, `mark_${new Date().getTime()}.xlsx`)
    },
    // 文件下载处理
    handleDownload(row) {
      let name = row.fileName;
      let nameList = name.split(',');
      // 循环下载多个文件
      for(let i=0;i<nameList.length;i++){
        let url = process.env.VUE_APP_BASE_API+nameList[i];
        console.log(url)
        let suffix = url.substring(url.lastIndexOf("."), url.length);
        console.log(suffix)
        const a = document.createElement('a')
        a.setAttribute('download', row.uploadTime + '_' + row.uploadBy + suffix)
        // a.setAttribute('target', '_blank')  打开新页面下载
        a.setAttribute('href', url)
        a.click()
      }
      this.$modal.msgSuccess("开始下载");
    },
    /** 预览按钮 */
    handlePreview(row) {
      let name = row.fileName;
      let url =  process.env.VUE_APP_BASE_API+ row.fileName;
      let nameList = name.split(',');
      if(nameList.length > 1){
        this.$modal.msgError("该文件不支持预览");
      }
      else{
        let suffix = url.substring(url.lastIndexOf("."), url.length);
        if(suffix === '.png' || suffix === '.jpg' || suffix === '.jpeg'
          || suffix === '.gif' || suffix === '.pdf'){
          window.open(url);
        }else{
          this.$modal.msgError("该文件不支持预览");
        }

      }
      //window.open("https://view.officeapps.live.com/op/view.aspx?src="+url)
    },
    /** 分数更新 */
    handleMark(row){
      this.form.userId = row.userId
      this.form.materialId = row.materialId
      this.form.score = row.score
      console.log(this.form)
      updateMark(this.form).then(response => {
        this.$modal.msgSuccess("评分已保存");
      });
    }
  }
};
</script>
